//
//  DVChoosePhotoAlbumViewController.m
//  ImagePicker
//
//  Created by csh on 16/7/24.
//  Copyright © 2016年 com.read. All rights reserved.
//
#import "DVImagePickerManager.h"
#import "DVChoosePhotoAlbumViewController.h"
#import "DVAssetModel.h"
#import "ChooseTableViewCell.h"
@interface DVChoosePhotoAlbumViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *results;
@property(nonatomic,strong)NSMutableArray *assetModelArray;
@property(nonatomic,strong)UIButton *coverBtn;
@end

@implementation DVChoosePhotoAlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self coverBtn];
    [self initTableView];

    [[DVImagePickerManager manager]getFetchResultWithCompletion:^(NSMutableArray *array) {
        self.results=array;
        [self.tableView reloadData];
    }];
    // Do any additional setup after loading the view.
}

#pragma - mark initTableView
- (void)initTableView
{
    self.tableView = [[UITableView alloc]init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight=60;
    self.tableView.frame=CGRectMake(0, -360, [UIScreen mainScreen].bounds.size.width,360);
    [self.coverBtn addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _results.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(cell == nil)
    {
        cell = [[ChooseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.collection = [self.results objectAtIndex:indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PHAssetCollection *collection=self.results[indexPath.row];
    PHFetchOptions *option = [[PHFetchOptions alloc] init];
    option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
    PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:collection options:option];
    if (self.assetModelArray.count) {
        [self.assetModelArray removeAllObjects];
    }
    if(result.count>0)
    {
        for (PHAsset *asset in result) {
            DVAssetModel *model=[[DVAssetModel alloc]init];
            model.asset=asset;
            model.select=NO;
            [self.assetModelArray addObject:model];
        }
    }

    if ([self.seletedDelegate respondsToSelector:@selector(choosePhotoAlbumViewController:didSelectedModelArray: title:)]) {
        [self.seletedDelegate choosePhotoAlbumViewController:self didSelectedModelArray:self.assetModelArray title:collection.localizedTitle];
    }
}
-(NSMutableArray *)assetModelArray{
    if (!_assetModelArray) {
        _assetModelArray=[NSMutableArray array];
    }
    return _assetModelArray;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    self.tableView.frame = self.view.bounds;
  
}
-(UIButton *)coverBtn{
    if (!_coverBtn) {
        _coverBtn =[[UIButton alloc]init];
        [_coverBtn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
        _coverBtn.backgroundColor=[UIColor colorWithWhite:0.0 alpha:0.2];
        _coverBtn.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        [self.view addSubview:_coverBtn];
    }
    return _coverBtn;
}
-(void)backClick:(UIButton *)btn{
    [self hidden];
}
-(void)show{
    self.view.hidden=NO;
    [UIView animateWithDuration:0.35 animations:^{
        self.tableView.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 360);
    }];
    self.sourceVC.isClick=YES;

}
-(void)hidden{
    [UIView animateWithDuration:0.35 animations:^{
        self.tableView.frame=CGRectMake(0, -360, [UIScreen mainScreen].bounds.size.width, 360);
    }completion:^(BOOL finished) {
        self.view.hidden=YES;
    }];
    self.sourceVC.isClick=NO;

}

@end

//@implementation PhotoAlbumCell
//- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
//{
//    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
//    if(self)
//    {
//        self.imageView.clipsToBounds=YES;
//    }
//    
//    return self;
//}
//
//- (void)setCollection:(PHAssetCollection *)collection
//{
//    _collection = collection;
//    PHFetchOptions *option = [[PHFetchOptions alloc] init];
//    option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld", PHAssetMediaTypeImage];
//    self.textLabel.text = collection.localizedTitle;
//    PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:collection options:option];
//    if(result.count>0)
//    {
//        PHAsset *asset = [result objectAtIndex:0];
//        [[DVImagePickerManager manager] getPhotoWithAsset:asset completion:^(UIImage *image, NSDictionary *info, BOOL isDegraded) {
//            self.imageView.image = image;
//        }];
//    }
//}

//@end
